package com.ymatou.apigateway.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;

import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.SqGetRefundBillListReq;
import com.ymatou.apigateway.service.OpenapiService;
import com.ymatou.apigateway.service.SqGetRefundBillListCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class Ts_SqGetRefundBillList {

	private static OpenapiReq openapiReq;
	private static SqGetRefundBillListCall sqGetRefundBillListCall;
	private static OpenApiWapper openApiWapper;
	private static SqGetRefundBillListReq sqGetRefundBillListReq;
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId")); 

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("openapi获取神器退单详情");
		openApiWapper = new OpenApiWapper();
	}

	@Before
	public void caseUp() {

		openapiReq = new OpenapiReq();
		
		sqGetRefundBillListReq = new SqGetRefundBillListReq();
		sqGetRefundBillListCall = new SqGetRefundBillListCall();
		
		openapiReq.setMethod("sq.refundbill.list.get");
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sqGetRefundBillListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SqGetRefundBillDetail_001() {
		Logger.start(true, "openapi获取神器退单详情");
		try {
			
			Long sellerId = autotest_goodsOwnerId;
			
			sqGetRefundBillListReq.setStart_date_time("2021-01-01 00:00:00");
			sqGetRefundBillListReq.setEnd_date_time("2021-01-31 00:00:00");
			sqGetRefundBillListReq.setPage_index(1);
			sqGetRefundBillListReq.setPage_size(10);

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqGetRefundBillListReq));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.refundbill.list.get",
							app_id);

			sqGetRefundBillListCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			sqGetRefundBillListCall.setData(openapiReq);

			sqGetRefundBillListCall.callService();

			Logger.verifyEquals("0000", sqGetRefundBillListCall.getCode(), 
					"校验code");
			Logger.verifyEquals("", sqGetRefundBillListCall.getMessage(),
					"校验message");

			Logger.verifyNotNull(sqGetRefundBillListCall.getContent(),
					"校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SqGetRefundBillDetail_002() {
		Logger.start(true, "openapi获取神器分销员退单详情");
		try {
			
			Long sellerId = autotest_goodsOwnerId;
			
			sqGetRefundBillListReq.setStart_date_time("2021-01-09 00:00:00");
			sqGetRefundBillListReq.setEnd_date_time("2021-01-10 00:00:00");
			sqGetRefundBillListReq.setPage_index(1);
			sqGetRefundBillListReq.setPage_size(10);

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqGetRefundBillListReq));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.refundbill.list.get",
							app_id);

			sqGetRefundBillListCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			sqGetRefundBillListCall.setData(openapiReq);

			sqGetRefundBillListCall.callService();

			Logger.verifyEquals("0000", sqGetRefundBillListCall.getCode(), 
					"校验code");
			Logger.verifyEquals("", sqGetRefundBillListCall.getMessage(),
					"校验message");

			Logger.verifyNotNull(sqGetRefundBillListCall.getContent(),
					"校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SqGetRefundBillDetail_003() {
		Logger.start(true, "openapi获取神器分销员退单详情，时间超过6个月");
		try {
			
			Long sellerId = autotest_goodsOwnerId;
			
			sqGetRefundBillListReq.setStart_date_time("2020-05-09 00:00:00");
			sqGetRefundBillListReq.setEnd_date_time("2021-01-10 00:00:00");
			sqGetRefundBillListReq.setPage_index(1);
			sqGetRefundBillListReq.setPage_size(10);

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqGetRefundBillListReq));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.refundbill.list.get",
							app_id);

			sqGetRefundBillListCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			sqGetRefundBillListCall.setData(openapiReq);

			sqGetRefundBillListCall.callService();

			Logger.verifyEquals("0001", sqGetRefundBillListCall.getCode(), 
					"校验code");
			Logger.verifyEquals("时间跨度最多180天", sqGetRefundBillListCall.getMessage(),
					"校验message");

			Logger.verifyNotNull(sqGetRefundBillListCall.getContent(),
					"校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static void main(String[] args) {
		
//		Ts_SqGetRefundBillDetail test = new Ts_SqGetRefundBillDetail();
	}
}
